package com.valveinspectionsys;



import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.valveinspectionsys.checkwork.activity.CheckWorkMainActivity;
import com.valveinspectionsys.dbhelp.XmlNameHelper;
import com.valveinspectionsys.maintain.activity.MaintainMainActivity;
import com.valveinspectionsys.order.activity.OrderMainActivity;
import com.valveinspectionsys.repair.activity.RepairMainActivity;
import com.valveinspectionsys.routing.activity.DeviceRoutingMainActivity;
import com.valveinspectionsys.rushrepair.RushRepairMainActivity;
import com.valveinspectionsys.settingsys.activity.WebActivity;
import com.valveinspectionsys.util.SharedPreferenceUtil;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;

/**
 * 进入APP首页展现所有功能，应用功能需登陆后才能使用
 * @author King
 * 2013-4-23
 *
 */
public class MainActivity extends Activity  implements OnClickListener,BDLocationListener{
    public static final int ITEM0 = Menu.FIRST;// 系统值
    public Button button1;//登录/注销
    public Button button2;//巡检
    public Button button3;//保养
    public Button button4;//维修
    public Button button5;//抢修
    public Button button6;//设置
    
    private static final int REQUEST_CODE=1;
    
    private final static int DIALOG = 1;
    
    public SQLiteDatabase db;

    public SharedPreferenceUtil sp;

    public LocationClient client;
    
    public BroadcastReceiver broadcastReceiver;
    


    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);
	sp=new SharedPreferenceUtil(this);
	  
	String gpsdate = sp.getValue(XmlNameHelper.GPSDATE);
	if (gpsdate == null || gpsdate.equals("")) {
	    sp.insertValue(XmlNameHelper.GPSDATE, "0");
	    sp.insertValue(XmlNameHelper.GPSLATITUDE, "0.0");
	    sp.insertValue(XmlNameHelper.GPSLONGITUDE, "0.0");
	}
      
	initLocation();
	
	initView();

	// 广播的内部类，当收到关闭事件时，调用finish方法结束activity
		broadcastReceiver = new BroadcastReceiver() {
		    @Override
		    public void onReceive(Context context, Intent intent) {
			sp.insertValue(XmlNameHelper.UnlockStatus, false);
			finish();
		    }
		};
		// 在当前的activity中注册广播
		IntentFilter filter = new IntentFilter();
		filter.addAction("com.app.exit");
		this.registerReceiver(broadcastReceiver, filter);
	if(TextUtils.isEmpty(sp.getValue(XmlNameHelper.IMEI))){
	  //获取手机IMEI号，并保存
		TelephonyManager tm = (TelephonyManager) getSystemService(MainActivity.TELEPHONY_SERVICE);
		sp.insertValue(XmlNameHelper.IMEI, tm.getDeviceId());
	}
	
	
    }
      //初始化控件，并添加点击事件
    public void initView(){
	button1=(Button)findViewById(R.id.checkwork);
	button2=(Button)findViewById(R.id.waterlist);
	button3=(Button)findViewById(R.id.usermanager);
	button4=(Button)findViewById(R.id.exitsys);
	button5=(Button)findViewById(R.id.Report);
	button6=(Button)findViewById(R.id.settingsys);
	button5.setOnClickListener(this);
	button6.setOnClickListener(this);
	button4.setOnClickListener(this);
	button3.setOnClickListener(this);
	button2.setOnClickListener(this);
	button1.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
	Intent intent=null;
	switch (v.getId()) {
	case R.id.checkwork:
	    if(!sp.getBoolean(XmlNameHelper.CHECKSERVERCONNECT)){
		Toast.makeText(MainActivity.this, "请设置连接服务器参数,点击菜单键进行设置", Toast.LENGTH_SHORT).show();
		return;
	    }
	    intent=new Intent(MainActivity.this, CheckWorkMainActivity.class);
	    startActivityForResult(intent, REQUEST_CODE);
	    break;
	case R.id.waterlist:
	    if(!sp.getBoolean(XmlNameHelper.workstatus)){
		Toast.makeText(MainActivity.this, "请登录后再使用", Toast.LENGTH_SHORT).show();
		return;
	    }
	    intent=new Intent(MainActivity.this, DeviceRoutingMainActivity.class);
	    startActivityForResult(intent, REQUEST_CODE);
	    break;
	case R.id.usermanager:
	    if(!sp.getBoolean(XmlNameHelper.workstatus)){
	  		Toast.makeText(MainActivity.this, "请登录后再使用", Toast.LENGTH_SHORT).show();
	  		return;
	  	    }
	     intent=new Intent(MainActivity.this, MaintainMainActivity.class);
	     startActivityForResult(intent, REQUEST_CODE);
	    break;
	case R.id.exitsys:
	    if(!sp.getBoolean(XmlNameHelper.workstatus)){
	  		Toast.makeText(MainActivity.this, "请登录后再使用", Toast.LENGTH_SHORT).show();
	  		return;
	  	    }
	      intent=new Intent(MainActivity.this, RepairMainActivity.class);
	      startActivityForResult(intent, REQUEST_CODE);
	    break;
	case R.id.Report:
	    if(!sp.getBoolean(XmlNameHelper.workstatus)){
	  		Toast.makeText(MainActivity.this, "请登录后再使用", Toast.LENGTH_SHORT).show();
	  		return;
	  	    }
	      intent=new Intent(MainActivity.this, RushRepairMainActivity.class);
	      startActivityForResult(intent, REQUEST_CODE);
	    break;
	case R.id.settingsys:
	    if(!sp.getBoolean(XmlNameHelper.workstatus)){
  		Toast.makeText(MainActivity.this, "请登录后再使用", Toast.LENGTH_SHORT).show();
  		return;
  	    }
//	
	 //   intent=new Intent(MainActivity.this, WebActivity.class);
	 //  startActivityForResult(intent, REQUEST_CODE);
	   intent=new Intent(MainActivity.this, OrderMainActivity.class);
	    startActivity(intent);
	    break;
	default:
	    break;
	}
	
    }
   
  
    //初始化定位参数，获取导航所需要的city
    public void initLocation() {
  	client = new LocationClient(getApplicationContext());
  	LocationClientOption option = new LocationClientOption();
  	// 打开GPS
  	option.setOpenGps(true);
  	// 设置坐标类型
  	option.setCoorType("bd09ll");

  	// 设置网络优先

  	option.setPriority(LocationClientOption.NetWorkFirst);
  	// 设置产品线名称
  	option.setProdName("waterSys");
  	option.setScanSpan(5000);
  	// 是否启用缓存定位
  	option.disableCache(true);
  	client.setLocOption(option);
  	client.registerLocationListener(this);
  	client.start();

      }
    @Override
    public void onReceiveLocation(BDLocation location) {
	

	if (location != null && location.getLocType() == 161) {
	    String city = location.getCity();
	    if (!TextUtils.isEmpty(city)) {
		city = city.substring(0, city.length() - 1);
		sp.insertValue(XmlNameHelper.CITY, city);
	    }

	    client.stop();
	}

    
    }
    @Override
    public void onReceivePoi(BDLocation arg0) {
	
	
    }
    @Override
    protected Dialog onCreateDialog(int id) {
	Dialog dialog = null;
	switch (id) {
	case DIALOG:
	    Builder builder = new android.app.AlertDialog.Builder(this);
	    // 设置对话框的图标
	    builder.setIcon(R.drawable.buttondraw);
	    // 设置对话框的标题
	    builder.setTitle("提示信息");
	    builder.setMessage("确定要退设备业务处理系统吗?");
	    // 0: 默认第一个单选按钮被选中
	    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {

		    sp.insertValue(XmlNameHelper.UnlockStatus, false);
		    MapApplication app = (MapApplication) MainActivity.this.getApplication();
		    if (app.mBMapMan != null) {
			app.mBMapMan.destroy();
			app.mBMapMan = null;
		    }

		    System.exit(0);

		}
	    });
	    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
		    dialog.dismiss();

		}
	    });

	    dialog = builder.create();
	    break;

	default:
	    break;
	}
	return dialog;
    }
    
    
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK) {
	    showDialog(DIALOG);
	    return false;
	}
	return super.onKeyUp(keyCode, event);
    }
    
    
   
 
    
    
    
    @Override
    protected void onDestroy() {

	if (client != null && client.isStarted()) {
	    client.stop();
	}
	
	this.unregisterReceiver(broadcastReceiver);
	    MapApplication app = (MapApplication) MainActivity.this.getApplication();
	    if (app.mBMapMan != null) {
		app.mBMapMan.destroy();
		app.mBMapMan = null;
	    }
	super.onDestroy();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	menu.add(0, ITEM0 , 0, "设置");

	menu.findItem(ITEM0 + 1);

	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

	switch (item.getItemId()) {

	case ITEM0 :
	    Intent    intent=new Intent(MainActivity.this, WebActivity.class);
	    startActivity(intent);
	    break;
	

	default:
	    break;
	}
	return super.onOptionsItemSelected(item);
    }
    
    
    
}
